package com.yoda.mall.admin.controller;

import com.yoda.mall.admin.service.ResourceService;
import com.yoda.mall.model.entity.ums.Resource;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 分页模糊查询资源
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @return
     */
    @GetMapping("list")
    public R<PageResult<Resource>> list( @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",required = false,defaultValue = "5")Integer pageSize,
                                         Integer  categoryId,String nameKeyword,String urlKeyword){
        PageResult<Resource> pageResult = resourceService.list(categoryId,nameKeyword,urlKeyword,pageNum,pageSize);
        return R.success(pageResult);
    }

    /**
     * 查询所有资源
     * @return
     */
    @GetMapping("listAll")
    public R<List<Resource>> listAll(){
        List<Resource> list = resourceService.listAll();
        return R.success(list);
    }

    /**
     * 添加后台资源
     * @param resource
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody Resource resource){
        resourceService.create(resource);
        return R.success(null);
    }

    /**
     *根据ID删除后台资源
     * @param id
     * @return
     */
    @PostMapping("delete/{id}")
    public R delete(@PathVariable Integer id){
        resourceService.delete(id);
        return R.success(null);
    }

    /**
     * 修改后台资源
     * @param id
     * @param resource
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Integer id,@RequestBody Resource resource){
        resourceService.update(id,resource);
        return R.success(null);
    }

    /**
     * 根据ID获取资源详情
     * @param id
     * @return
     */
    @PostMapping("{id}")
    public R<Resource> info(@PathVariable Integer id){
        Resource resource = resourceService.info(id);
        return R.success(resource);
    }
}
